
#import "MainController.h"
#import "BrowserWindowController.h"

@implementation MainController

- (void)openBrowserWindow:(id)sender {
    NSOpenPanel *openPanel = [NSOpenPanel openPanel];
    [openPanel setCanChooseDirectories:YES];
    [openPanel setCanChooseFiles:NO];
    int result = [openPanel runModalForDirectory:[@"~/Pictures" stringByExpandingTildeInPath] file:nil types:nil];
    if (result == NSOKButton) {
        [self openBrowserWindowForPath:[[openPanel filenames] objectAtIndex:0]];
    }
}

- (BOOL)openBrowserWindowForPath:(NSString *)path {
    BOOL isDirectory = NO;
    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory];
    if (exists && isDirectory) {
        BrowserWindowController *browserWindowController = [[BrowserWindowController alloc] initWithPath:path];
        if (browserWindowController) {
            [browserWindowController showWindow:self];
            return YES;
        }
    } else {
        [[NSAlert alertWithMessageText:@"Can't browse path" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:(exists ? @"Path exists but isn't a directory (%@)" : @"Path doesn't exist (%@)"), path] runModal];
    }
    return NO;
}

- (void)browseNatureDesktopPictures:(id)sender {
    [self openBrowserWindowForPath:@"/Library/Desktop Pictures/Nature/"];
}

- (void)browsePlantsDesktopPictures:(id)sender {
    [self openBrowserWindowForPath:@"/Library/Desktop Pictures/Plants/"];
}

- (void)browseBeachScreenSaverPictures:(id)sender {
    [self openBrowserWindowForPath:@"/System/Library/Screen Savers/Beach.slideSaver/Contents/Resources/"];
}

- (void)browseCosmosScreenSaverPictures:(id)sender {
    [self openBrowserWindowForPath:@"/System/Library/Screen Savers/Cosmos.slideSaver/Contents/Resources/"];
}

- (void)browseForestScreenSaverPictures:(id)sender {
    [self openBrowserWindowForPath:@"/System/Library/Screen Savers/Forest.slideSaver/Contents/Resources/"];
}

- (void)browseNaturePatternsScreenSaverPictures:(id)sender {
    [self openBrowserWindowForPath:@"/System/Library/Screen Savers/Nature Patterns.slideSaver/Contents/Resources/"];
}

- (void)browsePaperShadowScreenSaverPictures:(id)sender {
    [self openBrowserWindowForPath:@"/System/Library/Screen Savers/Paper Shadow.slideSaver/Contents/Resources/"];
}

@end

@implementation MainController (NSApplicationDelegateMethods)

// Suppress default behavior of opening an "Untitled" browser on launch.
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {
    return NO;
}

// Auto-open a browser on launch.
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
    // Try default path for WWDC demo first.
    NSString *path = [@"~/Pictures/All Desktop Pictures" stringByExpandingTildeInPath];
    BOOL isDirectory = NO;
    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory];
    if (exists && isDirectory) {
        [self openBrowserWindowForPath:path];
    } else {
        [self browseNatureDesktopPictures:self];
    }
}

@end
